/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.plian.system.controller.cqzc.existapproval;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.dto.fr.bonds.ExistApprovalDTO;
import com.plian.system.entity.fr.bonds.BondsApply;
import com.plian.system.entity.fr.bonds.BondsPlan;
import com.plian.system.entity.fr.bonds.ExistApproval;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.cqzc.bondsapply.BondsApplyService;
import com.plian.system.service.cqzc.existapproval.ExistApprovalService;
import com.plian.system.service.cqzc.plan.BondsPlanService;
import com.plian.system.vo.fr.bonds.ExistApprovalVO;
import com.plian.system.wrapper.fr.bonds.ExistApprovalWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 现有批文 控制器
 *
 * @author
 * @since 2019-09-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/existApproval")
@Api(value = "ExistApprovalController", tags = "现有批文 控制器")
public class ExistApprovalController extends BaseController {

	private ExistApprovalService existApprovalService;

	private ExistApprovalWrapper existApprovalWrapper;

	private BondsApplyService bondsApplyService;

	private BondsPlanService bondsPlanService;

	/**
	 * 详情
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入existApproval")
	public R<ExistApprovalVO> detail(@RequestBody ExistApproval existApproval) {
		ExistApproval detail = existApprovalService.getOne(Condition.getQueryWrapper(existApproval));
		if(null == detail) return R.fail(CommonCode.FAIL,"批文不存在");
		ExistApprovalVO vo = existApprovalWrapper.entityToVO(detail);
		BondsApply bondsApply = bondsApplyService.getOne(new LambdaQueryWrapper<BondsApply>().eq(BondsApply::getExistApprovalId, detail.getId()), false);
		if(null != bondsApply) vo.setBondsApplyId(bondsApply.getId());
		BondsPlan bondsPlan = bondsPlanService.getOne(new LambdaQueryWrapper<BondsPlan>().eq(BondsPlan::getExistApprovalId, detail.getId()), false);
		if(null != bondsPlan) vo.setBondsPlanId(bondsPlan.getId());
		return R.data(vo);
	}

	/**
	 * 分页
	 */
	@PostMapping("/list")
	@ApiOperation(value = "分页", notes = "传入query")
	public ListResult<ExistApprovalVO> list(@RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		if (StringUtils.isBlank(query.getDescs())){
			query.setDescs("create_time");
		}
		QueryWrapper<ExistApproval> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new ExistApproval()), entityList
		);
		TokenSearch<ExistApproval> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenNoFlowQuery(queryWrapper, query);
		IPage<ExistApproval> pages = existApprovalService.page(Condition.getPage(query), queryWrapper);
		List<ExistApprovalVO> existApprovalVOS = existApprovalWrapper.entityToVO(pages.getRecords());
		ListResult<ExistApprovalVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(existApprovalVOS);
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;
	}

	@PostMapping("/unusedApplyList")
	@ApiOperation(value = "分页", notes = "传入query")
	public ListResult<ExistApprovalVO> unusedApplyList(@RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		if (query.getAscs()==null&&query.getDescs()==null){
			query.setDescs("create_time");
		}
		QueryWrapper<ExistApproval> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new ExistApproval()), entityList
		);
		queryWrapper.apply("NOT EXISTS (SELECT 1 FROM cqzc_bonds_apply b WHERE b.exist_approval_id = cqzc_exist_approval.id)");
		TokenSearch<ExistApproval> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenNoFlowQuery(queryWrapper, query);
		IPage<ExistApproval> pages = existApprovalService.page(Condition.getPage(query), queryWrapper);
		List<ExistApprovalVO> existApprovalVOS = existApprovalWrapper.entityToVO(pages.getRecords());
		ListResult<ExistApprovalVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(existApprovalVOS);
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;
	}

	@PostMapping("/unusedPlanList")
	@ApiOperation(value = "分页", notes = "传入query")
	public ListResult<ExistApprovalVO> unusedPlanList(@RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		if (query.getAscs()==null&&query.getDescs()==null){
			query.setDescs("create_time");
		}
		QueryWrapper<ExistApproval> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new ExistApproval()), entityList
		);
		queryWrapper.apply("NOT EXISTS (SELECT 1 FROM cqzc_bonds_plan b WHERE b.exist_approval_id = cqzc_exist_approval.id)");
		TokenSearch<ExistApproval> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenNoFlowQuery(queryWrapper, query);
		IPage<ExistApproval> pages = existApprovalService.page(Condition.getPage(query), queryWrapper);
		List<ExistApprovalVO> existApprovalVOS = existApprovalWrapper.entityToVO(pages.getRecords());
		ListResult<ExistApprovalVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(existApprovalVOS);
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;
	}
	/**
	 * 新增
	 */
	@PostMapping("/save")
	@Resubmit(delaySeconds = 0)
	@LogOperation("现有批文-保存")
	@ApiOperation(value = "新增", notes = "传入existApproval")
	public R<ExistApproval> save(@Valid @RequestBody ExistApprovalDTO dto) {

		ExistApproval existApproval;
		try {
			existApproval = existApprovalService.saveExistApproval(dto);
		} catch (Exception e) {
			return R.fail(CommonCode.FAIL,e.getMessage());
		}

		return R.data(existApproval);
	}

	/**
	 * 编辑
	 */
	@PostMapping("/edit")
	@Resubmit(delaySeconds = 0)
	@LogOperation("现有批文-编辑")
	@ApiOperation(value = "编辑", notes = "传入existApproval")
	public R<ExistApproval> edit(@Valid @RequestBody ExistApprovalDTO dto) {

		ExistApproval existApproval;
		try {
			existApproval = existApprovalService.editExistApproval(dto);
		} catch (Exception e) {
			return R.fail(CommonCode.FAIL,e.getMessage());
		}

		return R.data(existApproval);
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@LogOperation("现有批文-删除")
	@ApiOperation(value = "删除", notes = "传入 ids")
	public R remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		try {
			existApprovalService.delByIds(ids);
		} catch (Exception e) {
			return R.fail(CommonCode.FAIL,e.getMessage());
		}
		return new R(CommonCode.SUCCESS);
	}


}
